#!/bin/bash

# check if Bazzite or SteamOS
grep -i bazzite /etc/os-release &> /dev/null
if [ $? -eq 0 ]
then
	OS=bazzite
	EFI_PATH=/boot/efi/EFI
	BOOTX64=$EFI_PATH/BOOT/BOOTX64.EFI
else
	grep -i SteamOS /etc/os-release &> /dev/null
	if [ $? -eq 0 ]
	then
		OS=SteamOS
		EFI_PATH=/esp/efi
		BOOTX64=$EFI_PATH/boot/bootx64.efi
	else
		exit
	fi
fi

current_password=$(zenity --password --title "密码默认1234")
echo -e "$current_password\n" | sudo -S ls &> /dev/null
if [ $? -ne 0 ]
then
	echo sudo 密码错误！| \
		zenity --text-info --title "双系统引导配置工具" --width 400 --height 200
	exit
fi

while true
do
Choice=$(zenity --width 750 --height 450 --list --radiolist --multiple 	--title "淘宝：SteamDeck 超人 双系统引导配置工具"\
	--column "选择" \
	--column "选项" \
	--column="备注-请仔细阅读!"\
	FALSE 本机信息 "在提交bugs时选择此项!"\
	FALSE Batocera "在Batocera v39（及更新的版本）或v38（及旧版本）之间选择配置."\
	FALSE 主题 "此项目勿选."\
	FALSE 倒计时 "倒计时1 5 10或者15秒后自动引导默认系统."\
	FALSE 服务 "关闭/启用Clover EFI条目和Clover systemd服务."\
	FALSE 引导 "设置默认情况下将启动的操作系统."\
	FALSE 启动Logo "更换启动Logo."\
	False 无启动Logo "开机无启动Logo."\
	FALSE 分辨率 "如果使用的是1200P以上的分辨率，则开启此选项."\
	FALSE 自定义 "隐藏引导页面OPTIONS选项."\
	FALSE 卸载 "卸载双系统引导并还原所做的所有更改 ."\
	TRUE 退出 "***** 退出SteamDeck超人 双系统引导配置工具 *****")


if [ $? -eq 1 ] || [ "$Choice" == "退出" ]
then
	echo User pressed CANCEL / EXIT.
	exit

elif [ "$Choice" == "本机信息" ]
then
	zenity --warning --title "双系统引导配置工具" --text "$(fold -w 120 -s ~/1Clover-tools/status.txt)" --width 1000 --height 400

elif [ "$Choice" == "Batocera" ]
then
Batocera_Choice=$(zenity --width 550 --height 220 --list --radiolist --multiple --title "Clover Toolbox" --column "Select One" \
	--column "选项" --column="备注-请仔细阅读!"\
	FALSE v39 "为Batocera v39及更新版本设置引导配置."\
	FALSE v38 "为Batocera v38及更早版本设置引导配置."\
	TRUE EXIT "***** 返回上一级 *****")

	if [ $? -eq 1 ] || [ "$Batocera_Choice" == "EXIT" ]
	then
		echo 用户选择[取消].返回主菜单.

	elif [ "$Batocera_Choice" == "v39" ]
	then
		# Update the config.plist for Batocera v39 and newer
		echo -e "$current_password\n" | sudo -S sed -i '/<string>os_batocera<\/string>/!b;n;n;c\\t\t\t\t\t<string>\\EFI\\batocera\\grubx64\.efi<\/string>' $EFI_PATH/clover/config.plist

		zenity --warning --title "双系统引导配置工具" --text "Batocera v39及更高版本的Clover配置已经更新！" --width 450 --height 75

	elif [ "$Batocera_Choice" == "v38" ]
	then
		# Update the config.plist for Batocera v38 and older
		echo -e "$current_password\n" | sudo -S sed -i '/<string>os_batocera<\/string>/!b;n;n;c\\t\t\t\t\t<string>\\EFI\\BOOT\\BOOTX64\.efi<\/string>' $EFI_PATH/clover/config.plist

		zenity --warning --title "双系统引导配置工具" --text "Batocera v38及更早版本的Clover配置已更新!" --width 450 --height 75

	fi

elif [ "$Choice" == "主题" ]
then
Theme_Choice=$(zenity --title "双系统引导配置工具"	--width 200 --height 325 --list \
	--column "主题名字" $(echo -e "$current_password\n" | sudo -S ls $EFI_PATH/clover/themes) )

	if [ $? -eq 1 ]
	then
		echo 用户选择[取消].返回主菜单.
	else
		echo -e "$current_password\n" | sudo -S sed -i '/<key>Theme<\/key>/!b;n;c\\t\t<string>'$Theme_Choice'<\/string>' $EFI_PATH/clover/config.plist
		zenity --warning --title "双系统引导配置工具" --text "主题已更改为$Theme_Choice!" --width 400 --height 75
	fi

elif [ "$Choice" == "倒计时" ]
then
Timeout_Choice=$(zenity --width 500 --height 300 --list --radiolist --multiple 	--title "双系统引导配置工具" --column "选择" --column "选项" --column="备注-请仔细阅读!"\
	FALSE 1 "将默认时间设置为1秒."\
	FALSE 5 "将默认时间设置为5秒."\
	FALSE 10 "将默认时间设置为10秒."\
	FALSE 15 "将默认时间设置为15秒."\
	TRUE EXIT "***** 返回上一级 *****")


	if [ $? -eq 1 ] || [ "$Timeout_Choice" == "EXIT" ]
	then
		echo 用户选择[取消].返回主菜单.
	else
		# change the Default Timeout in config.plist 
		echo -e "$current_password\n" | sudo -S sed -i '/<key>Timeout<\/key>/!b;n;c\\t\t<integer>'$Timeout_Choice'<\/integer>' $EFI_PATH/clover/config.plist
		zenity --warning --title "双系统引导配置工具" --text "默认倒计时现在设置为 $Timeout_Choice !" --width 400 --height 75
	fi

elif [ "$Choice" == "服务" ]
then
Service_Choice=$(zenity --width 650 --height 250 --list --radiolist --multiple --title "双系统引导配置工具"\
	--column "选择" --column "选项" --column="备注-请仔细阅读!"\
	FALSE Disable "禁用Clover EFI条目和Clover systemd服务."\
	FALSE Enable "启用Clover EFI条目和Clover systemd服务."\
	TRUE EXIT "***** 返回上一级 *****")

	if [ $? -eq 1 ] || [ "$Service_Choice" == "EXIT" ]
	then
		echo 用户选择[取消].返回主菜单.

	elif [ "$Service_Choice" == "Disable" ]
	then
		# restore Windows EFI entry from backup
		echo -e "$current_password\n" | sudo -S cp $EFI_PATH/Microsoft/Boot/bootmgfw.efi.orig $EFI_PATH/Microsoft/Boot/bootmgfw.efi

		# make Windows the next boot option!
		Windows=$(efibootmgr | grep -i Windows | colrm 9 | colrm 1 4)
		echo -e "$current_password\n" | sudo -S efibootmgr -n $Windows &> /dev/null

		# disable the Clover systemd service
		echo -e "$current_password\n" | sudo -S systemctl disable --now clover-bootmanager
		zenity --warning --title "双系统引导配置工具" --text "双系统引导服务已被禁用。Windows现已激活！" --width 500 --height 75

	elif [ "$Service_Choice" == "Enable" ]
	then
		# enable the Clover systemd service
		sudo systemctl enable --now clover-bootmanager
		echo -e "$current_password\n" | sudo -S /etc/systemd/system/clover-bootmanager.sh
		zenity --warning --title "双系统引导配置工具" --text "双系统引导服务已启用。Windows默认启动现在被禁用！" --width 500 --height 75
	fi

elif [ "$Choice" == "引导" ]
then
Boot_Choice=$(zenity --width 550 --height 300 --list --radiolist --multiple --title "双系统引导配置工具" --column "选择" \
	--column "选项" --column="备注-请仔细阅读!"\
	FALSE Windows "将Windows设置为默认启动系统."\
	FALSE SteamOS "将SteamOS设置为默认启动系统."\
	FALSE Bazzite "将Bazzite设置为默认启动系统."\
	FALSE LastOS "将上一次启动的操作系统设置为默认启动系统."\
	TRUE EXIT "***** 返回上一级 *****")

	if [ $? -eq 1 ] || [ "$Boot_Choice" == "EXIT" ]
	then
		echo 用户选择[取消].返回主菜单.

	elif [ "$Boot_Choice" == "Windows" ]
	then
		# change the Default Loader to Windows in config.plist 

		echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultLoader<\/key>/!b;n;c\\t\t<string>\\EFI\\MICROSOFT\\bootmgfw\.efi<\/string>' $EFI_PATH/clover/config.plist
		echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultVolume<\/key>/!b;n;c\\t\t<string>esp<\/string>' $EFI_PATH/clover/config.plist

		zenity --warning --title "双系统引导配置工具" --text "Windows现在是双系统引导中的默认启动项!" --width 400 --height 75

	elif [ "$Boot_Choice" == "SteamOS" ]
	then
		# change the Default Loader in config.plist 
		echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultLoader<\/key>/!b;n;c\\t\t<string>\\EFI\\STEAMOS\\STEAMCL\.efi<\/string>' $EFI_PATH/clover/config.plist
		echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultVolume<\/key>/!b;n;c\\t\t<string>esp<\/string>' $EFI_PATH/clover/config.plist
		zenity --warning --title "双系统引导配置工具" --text "SteamOS现在是双系统引导中的默认启动项!" --width 400 --height 75

	elif [ "$Boot_Choice" == "Bazzite" ]
	then
		# change the Default Loader in config.plist 
		echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultLoader<\/key>/!b;n;c\\t\t<string>\\EFI\\FEDORA\\shimx64\.efi<\/string>' $EFI_PATH/clover/config.plist
		echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultVolume<\/key>/!b;n;c\\t\t<string>esp<\/string>' $EFI_PATH/clover/config.plist
		zenity --warning --title "双系统引导配置工具" --text "Bazzite现在是双系统引导中的默认启动项!" --width 400 --height 75

	elif [ "$Boot_Choice" == "LastOS" ]
	then
		# change the Default Volume in config.plist 
		echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultVolume<\/key>/!b;n;c\\t\t<string>LastBootedVolume<\/string>' $EFI_PATH/clover/config.plist
		zenity --warning --title "双系统引导配置工具" --text "上次最后使用的操作系统现在是双系统引导中的默认启动项!" --width 425 --height 75
	fi

elif [ "$Choice" == "启动Logo" ]
then
Logo_Choice=$(zenity --title "双系统引导配置工具"	--width 200 --height 350 --list \
	--column "Logo  名字" $(ls -l ~/1Clover-tools/logos/*.png | sed s/^.*\\/\//) )  
	if [ $? -eq 1 ]
	then
		echo 用户选择[取消].返回主菜单.
	else
		echo -e "$current_password\n" | sudo -S cp ~/1Clover-tools/logos/$Logo_Choice $EFI_PATH/steamos/steamos.png
		zenity --warning --title "双系统引导配置工具" --text "开机Logo已改为$Logo_Choice!" --width 400 --height 75
	fi

elif [ "$Choice" == "无启动Logo" ]
then
	echo -e "$current_password\n" | sudo -S rm $EFI_PATH/steamos/steamos.png &> /dev/null
	zenity --warning --title "双系统引导配置工具" --text "已恢复系统默认Logo!" --width 400 --height 75

elif [ "$Choice" == "分辨率" ]
then
Resolution_Choice=$(zenity --width 550 --height 250 --list --radiolist --multiple --title "双系统引导配置工具"\
	--column "选择" --column "选项" --column="备注-请仔细阅读!"\
	FALSE 800p "使用默认屏幕分辨率1280x800（选此项）."\
	FALSE 1200p "使用DeckHD屏幕分辨率1920x1200."\
	TRUE EXIT "***** 返回上一级 *****")


	if [ $? -eq 1 ] || [ "$Resolution_Choice" == "EXIT" ]
	then
		echo 用户选择[取消].返回主菜单.

	elif [ "$Resolution_Choice" == "800p" ]
	then
		# change the sceen resolution to 1280x800 in config,plist 
		echo -e "$current_password\n" | sudo -S sed -i '/<key>ScreenResolution<\/key>/!b;n;c\\t\t<string>1280x800<\/string>' $EFI_PATH/clover/config.plist
		zenity --warning --title "双系统引导配置工具" --text "屏幕分辨率现在设置为1280x800." --width 400 --height 75

	elif [ "$Resolution_Choice" == "1200p" ]
	then
		# change the sceen resolution to 1920x1200 in config,plist 
		echo -e "$current_password\n" | sudo -S sed -i '/<key>ScreenResolution<\/key>/!b;n;c\\t\t<string>1920x1200<\/string>' $EFI_PATH/clover/config.plist
		zenity --warning --title "双系统引导配置工具" --text "屏幕分辨率现在设置为1920x1200." --width 400 --height 75
	fi

elif [ "$Choice" == "自定义" ]
then
	echo -e "$current_password\n" | sudo -S cp ~/1Clover-tools/efi/custom_clover_5157.efi $EFI_PATH/clover/cloverx64.efi
	zenity --warning --title "双系统引导配置工具" --text "OPTIONS选项已隐藏!" --width 400 --height 75

elif [ "$Choice" == "卸载" ]
then
	# restore Windows EFI entry from backup
	echo -e "$current_password\n" | sudo -S mv $EFI_PATH/Microsoft/Boot/bootmgfw.efi.orig $EFI_PATH/Microsoft/Boot/bootmgfw.efi
	if [ $? -eq 0 ]
	then
		echo -e "$current_password\n" | sudo -S rm $EFI_PATH/Microsoft/bootmgfw.efi
	else
		echo -e "$current_password\n" | sudo -S mv $EFI_PATH/Microsoft/bootmgfw.efi $EFI_PATH/Microsoft/Boot/bootmgfw.efi
	fi
	echo -e "$current_password\n" | sudo -S mv $BOOTX64.orig $BOOTX64

	# remove Clover from the EFI system partition
	echo -e "$current_password\n" | sudo -S rm -rf $EFI_PATH/clover

	for entry in $(efibootmgr | grep "Clover - GUI" | colrm 9 | colrm 1 4)
	do
		echo -e "$current_password\n" | sudo -S efibootmgr -b $entry -B &> /dev/null
	done

	# remove custom logo / BGRT
	echo -e "$current_password\n" | sudo -S rm $EFI_PATH/steamos/steamos.png &> /dev/null

	# delete systemd service
	echo -e "$current_password\n" | sudo -S steamos-readonly disable
	echo -e "$current_password\n" | sudo -S systemctl stop clover-bootmanager.service
	echo -e "$current_password\n" | sudo -S rm /etc/systemd/system/clover-bootmanager*
	echo -e "$current_password\n" | sudo -S sudo systemctl daemon-reload
	echo -e "$current_password\n" | sudo -S steamos-readonly enable

	# delete dolphin root extension
	rm ~/.local/share/kservices5/ServiceMenus/open_as_root.desktop

	rm -rf ~/SteamDeck-Clover-dualboot
	rm -rf ~/1Clover-tools/
	rm ~/Desktop/双系统引导配置工具
	rm ~/Desktop/Clover-Toolbox

	zenity --warning --title "双系统引导配置工具" --text "双系统引导已被卸载，Windows EFI条目已被激活!" --width 600 --height 75
	exit
fi
done
